package net.thetabx.ftdba.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;


import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONObject;

public class ProxifiedHttpManager implements HttpManager {

	private Boolean ready = false;
	private Boolean connected = false;
		
	private String protocol = "https://";
	private String baseUrl = "thetabx.net/proxy";
	private String token;
	private DefaultHttpClient client = null;
	
	public ProxifiedHttpManager() {
		this.initHttpClient();
	}
	
	private void initHttpClient() {
		ready = true;
		client = new DefaultHttpClient();
	}
	
	@SuppressWarnings("unchecked")
	public HttpData sendPost(String url, String[][] postData) {
		if(client == null)
			initHttpClient();
		if(url.indexOf("/") != 0)
			url = "/" + url;
		HttpPost postMethod = new HttpPost(protocol + baseUrl + "/post/");
		
		JSONObject obj=new JSONObject();
		for(int i = 0; i < postData.length; i++) {
			obj.put((String)postData[i][0], (String)postData[i][1]);
		}
		obj.put("_url", url);
		obj.put("_token", token);
		try {
			postMethod.setEntity(new StringEntity(obj.toJSONString()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		HttpResponse response = null;
		try {
			response = client.execute(postMethod);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return new ProxifiedHttpData(response);
	}
	
	public HttpData sendGet(String url, String[][] getData) {
		if(url.indexOf("/") != 0)
			url = "/" + url;
		String eList = "";
		if(getData != null) {
			for(int i = 0; i < getData.length; i++) {
				eList += "&" + getData[i][0] + "=" + getData[i][1];
			}
		}

		return sendPost(url + eList, getData);
	}
	
	@SuppressWarnings("unchecked")
	public InputStream download(String url) {
		if(client == null)
			initHttpClient();
		if(url.indexOf("/") != 0)
			url = "/" + url;
		HttpPost postMethod = new HttpPost(protocol + baseUrl + "/post/");
		
		JSONObject obj = new JSONObject();
		obj.put("_url", url);
		obj.put("_token", token);
		try {
			postMethod.setEntity(new StringEntity(obj.toJSONString()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		HttpResponse response = null;
		try {
			response = client.execute(postMethod);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			return response.getEntity().getContent();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public HttpData send(String url) {
		return this.sendGet(url, null);
	}
	
	public void setSecure(Boolean secure) {
		if(secure)
			protocol = "https://";
		else
			protocol = "http://";
	}
	
	public void setCookie(String token) {
		this.token = token;
	}
	
	public String getCookie() {
		return token;
	}
	
	public void destroyHttpClient() {
		ready = false;
		connected = false;
		if(client == null)
			return;
		client = null;
	}
	
	public Boolean isReady() {
		return ready;
	}
	
	public void setConnected(Boolean connected) {
		this.connected = connected;
	}
	
	public Boolean isConnected() {
		return connected;
	}
	
	public Boolean isProxified() {
		return true;
	}

	@SuppressWarnings("unchecked")
	public HttpData connect(String username, String password) {
		if(client == null)
			initHttpClient();
		HttpPost postMethod = new HttpPost(protocol + baseUrl + "/init/");
		
		JSONObject obj=new JSONObject();
		obj.put("username", username);
		obj.put("password", password);
		try {
			postMethod.setEntity(new StringEntity(obj.toJSONString()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		HttpResponse response = null;
		try {
			response = client.execute(postMethod);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return new ProxifiedHttpData(response);
	}
}
